package ltd.followkba.comment.utils;

import java.util.ArrayList;
import java.util.List;

import ltd.advskin.MRAM;
import ltd.advskin.utils.SysUtils;
import ltd.followkba.comment.bean.BAccount;
import ltd.followkba.comment.bean.BSongInfo;
import ltd.followkba.comment.bean.BTaskRun;
import ltd.followkba.comment.bean.BUserInfo;
import ltd.followkba.comment.KgAppBase;
import ltd.followkba.comment.MBAPP;
import ltd.followkba.comment.MKCS;
import main.MAPI;

import static ltd.followkba.comment.MKCC.FILE_REFRESH;
import static ltd.followkba.comment.MKCC.cKCB_SW_CHANGBA;
import static ltd.followkba.comment.MKCC.cKCB_SW_MAXIMAL;
import static ltd.advskin.MSKIN.MSTRING;

/**
 * Created by Administrator on 2018/6/22.
 */

public class CMoreUtils {
    public void assignAllTask(){
        if (MKCS.gAssignTask == 1) {
            for (int counter = 0; counter < cKCB_SW_MAXIMAL; counter++) {
                MKCS.mCMultiThread[counter].stopTask();
            }
            MKCS.gAssignTask = 2;
        }
        if (MKCS.gAssignTask == 2) {
            MKCS.gAssignTask = 3;
            for (int counter = 0; counter < cKCB_SW_MAXIMAL; counter++) {
//                MSTRING("CMoreUtils assignAllTask AA "+counter+" "+MKCS.mCMultiThread[counter].isTaskStop());
                if (!MKCS.mCMultiThread[counter].isTaskStop()){
                    MKCS.gAssignTask = 2;
                    return;
                }
            }
        }
        if (MKCS.gAssignTask == 3) {

            //MSTRING("AssignAllTask 1 "+MKCS.g4SetMaxTask);
            for (byte appType = 0; appType <  cKCB_SW_MAXIMAL; appType++){
                MKCS.mBTaskRuns[appType] = new ArrayList<>();
                List<BAccount> lists = MBAPP.getTaskAccountList(appType);
                int length = lists.size();
                BAccount bAccount;
              //  long timeMillis = System.currentTimeMillis();
                for (int counter = 0; counter < length; counter++) {
                    bAccount = lists.get(counter);
                    if (bAccount != null) {
                        if (appType == cKCB_SW_CHANGBA && counter < 5) {
//                            MSTRING("AssignAllTask BB " + bAccount.TaskID + " " + bAccount.AccountName + " " + bAccount.Password);
                        }
                        MKCS.gsWorkStatus[appType] = "正在分配任务 " + bAccount.TaskID;
                        BTaskRun bTaskRun = new BTaskRun();
                        bTaskRun.TaskID = bAccount.TaskID;
                        bTaskRun.AppType = appType;
//                        bTaskRun.OwnApp = MKCS.SwitchBaseApp(appType);
//                        bTaskRun.mBAccount = lists.get(counter);
//                        bTaskRun.StepCounter = bTaskRun.mBAccount.StepTime;
//                        bTaskRun.mOwnDetail = MBAPP.getUserDetail(appType, bTaskRun.mBAccount.UserID);
////                        MSTRING(String.format("AssignAllTask CC %d %s %s", MAPI.GET_FILE_LENGTH(bTaskRun.mOwnDetail.HeadFile), bTaskRun.mBAccount.UserID, bTaskRun.mOwnDetail.HeadFile));
//                        bTaskRun.OwnApp.LoginFlag = bTaskRun.OwnApp.Base64MixAccount(bTaskRun.mBAccount.MixToken);
//                        bTaskRun.OwnApp.SwitchUser(bTaskRun.OwnApp.LoginFlag, "", "");
//                        DbsTypeUserID dbsTypeUserID = new DbsTypeUserID(TASK_ACCOUNT, bTaskRun.AppType);
//                        bTaskRun.SuccessQty = dbsTypeUserID.getMemoryInt("SuccessQty");
////                        MSTRING(String.format("AssignAllTask CC %s %s _ %s", bTaskRun.mBAccount.AccountName, bTaskRun.mBAccount.MixToken, bTaskRun.OwnApp.LoginFlag));
////                        MSTRING(String.format("AssignAllTask DD %d %s %s ", appType, bTaskRun.mOwnDetail.UserName, bTaskRun.mBAccount.AccountStatus));
//                        if (!"ok".equals(bTaskRun.mBAccount.AccountStatus)){
//                            TaskCheckLogin(bTaskRun);
//                        }
//                        bTaskRun.OwnApp.downloadHeadFile(bTaskRun.mOwnDetail);
//                        MKCS.mBTaskRuns[appType].add(bTaskRun);
                    }
                }
            }
            MKCS.gTaskUpdate = 0x7f;
            MKCS.gAssignTask = 0;
        }
    }
    public boolean TaskCheckLogin(BTaskRun bTaskRun){
//        String gsLocal_1 = bTaskRun.OwnApp.LoginFlag;//bOwnerRun.mBAccount.MixToken;
//        if (gsLocal_1.length() > 12) {
//            String gsLocal_2 = bTaskRun.OwnApp.Base64MixAccount(gsLocal_1.substring(1, gsLocal_1.length()));
//            bTaskRun.OwnApp.SwitchUser(gsLocal_1, "", "");
//            String gsLocal_3 = bTaskRun.OwnApp.CheckLogin();
//            MSTRING("TaskCheckLogin B "+ gsLocal_1+"_"+gsLocal_2+"_"+gsLocal_3);
//            if ("ok".equals(gsLocal_3)) {
////                MREG.TaskReadMemoryFromFile(bOwnerRun);
//                bTaskRun.mBAccount.AccountStatus = "ok";
////                    MAPI.WR_PRIVATE_STRING(gsLocal_1, "ok", gsAppAccountFile);
//                return true;
//            }
//        }
//                gsLocal_2 = bOwnerRun.OwnApp.CheckLogin();

//        String[] account = bOwnerRun.LoginAccount.split("----");
//        String gsLocal_1;// = String.format("%d_%s", bOwnerRun.AppType, bOwnerRun.mOwnDetail.UserIdS);
//        String gsLocal_2;
//        if (bOwnerRun.OwnApp != null && account.length >= 3) {
//            if (account[2].indexOf(' ') < 0) {
//                gsLocal_1 = bOwnerRun.OwnApp.Base64MixAccount(account[2].substring(1, account[2].length()));
//                bOwnerRun.OwnApp.SwitchUser(gsLocal_1, "", "");
//                gsLocal_2 = bOwnerRun.OwnApp.CheckLogin();
//MSTRING("TaskCheckLogin B "+ bOwnerRun.LoginAccount+"_"+gsLocal_1+"_"+gsLocal_2);
//                if ("ok".equals(gsLocal_2)) {
//                    MREG.TaskReadMemoryFromFile(bOwnerRun);
//                    bOwnerRun.AccountStatus = "ok";
////                    MAPI.WR_PRIVATE_STRING(gsLocal_1, "ok", gsAppAccountFile);
//                    return true;
//                } else if (gsLocal_2.indexOf("账号") >= 0) {     // .startsWith("帐号异常")
//                    bOwnerRun.AccountStatus = gsLocal_2;
////                    MAPI.WR_PRIVATE_STRING(gsLocal_1, gsLocal_2, gsAppAccountFile);
//                    return false;
//                } else {
//                    MSTRING(gsLocal_1 + "检查登录出错：" + bOwnerRun.AccountStatus + " "+ bOwnerRun.OwnApp.NetworkError);
//                }
//            }
//        }
//        if (bOwnerRun.OwnApp != null && account.length >= 2 && !"null".equals(account)) {
//MSTRING("cc " + account[0] + " " + account[1] + " " + bOwnerRun.AppType);
//            gsLocal_2 = bOwnerRun.OwnApp.LoginSvr(account[0], account[1], "");
//            if ("ok".equals(gsLocal_2)) {
//                if (UploadAppLoginToken(bOwnerRun.Index, bOwnerRun, account[0], account[1])) {
//                    TaskReadMemoryFromFile(bOwnerRun);
//                    bOwnerRun.AccountStatus = "ok";
//                }
//                return true;
//            }
//            else {
//                bOwnerRun.AccountStatus = gsLocal_2;
//            }
//        }
        return false;
    }


    public void CreateSongList(int mode, int AppType, BUserInfo aUserInfo, byte[]done){ // 0=正常，1＝重新开始，2=更多
        final int mMode = mode;
        final int mAppType = AppType;
        final byte[] mDone = done;
        final BUserInfo mAUserInfo = aUserInfo;
        MSTRING("CreateSongList A "+mode+" "+AppType);
        Runnable runnable = new Runnable() {
            public void run() {
                InfoFile infoFile = new InfoFile(mAppType, mAUserInfo.userID + ".ini");
                InfoFile newUserInfoFile = new InfoFile(mAppType, FILE_REFRESH);
                newUserInfoFile.DeleteAllInfoID();
                KgAppBase kgAppBase = MKCS.CloneNewBaseApp(mAppType);
                byte gDone = 2;
//                MSTRING("CreateSongList 0 "+mMode+" "+kgAppBase);
                if (kgAppBase != null && mAUserInfo != null) {
                    int g4Local_1 = 0;
                    int mode = mMode;
                    String gsLocal_2;
                    BUserInfo aUserInfo;
                    BSongInfo aSongInfo;
                    if (mode == 0 && mAUserInfo.songIDs != null){            // 0=正常，1＝重新开始，2=更多
                        MSTRING("CreateSongList A "+mode+" "+mAUserInfo.userID+" "+mAUserInfo.songIDs.size());
                        gDone = 1;
                        while (g4Local_1 < mAUserInfo.songIDs.size()) {
                            gsLocal_2 = mAUserInfo.songIDs.get(g4Local_1);
                            aSongInfo = (BSongInfo)infoFile.GetInfoFromFile(gsLocal_2);
//MSTRING("CreateSongList C " + g4Local_1 + " "+gsLocal_2+" "+aSongInfo);
                            if (aSongInfo != null && !gsLocal_2.equals(aSongInfo.songID)){
                                aSongInfo = kgAppBase.GetSongInfo(gsLocal_2);
                                infoFile.AddInfoToFile(aSongInfo, gsLocal_2);
//                                MSTRING("mUserSongFile.AddInfoToFile 0 " + g4Local_1 + " " + gsLocal_2 + " " + aSongInfo.WorksName);
                            }
                            else if (aSongInfo == null){
                                gDone = 2;
//MSTRING("CreateSongList 5 " + gsLocal_2);
                            }
                            ++g4Local_1;
                        }
                        MSTRING("CreateSongList B "+gDone);
                        if (gDone == 2){
                            mode = 1;
                            g4Local_1 = 0;  // 0410a
                        }
                    }
                    if (mode == 1){       // 0=正常，1＝重新开始，2=更多
                        aUserInfo = kgAppBase.GetUserFullInfo(mAUserInfo.userID, null);
//MSTRING("CreateSongList D "+aUserInfo+" "+aUserInfo.IdS.size()+" "+aUserInfo);
                        if (aUserInfo != null && aUserInfo.songIDs != null) {
                            gDone = 1;
                            newUserInfoFile.AddInfoToFile(aUserInfo, aUserInfo.userID);
//MSTRING("CreateSongList E " + g4Local_1 + " " + aUserInfo.IdS.size());
                            while (g4Local_1 < aUserInfo.songIDs.size()) {
                                gsLocal_2 = aUserInfo.songIDs.get(g4Local_1);
                                aSongInfo = kgAppBase.GetSongInfo(gsLocal_2);
//                                MSTRING("CreateSongList E " + g4Local_1 + " " + gsLocal_2 + " " + aSongInfo.WorksName);
                                if (aSongInfo != null){
                                    infoFile.AddInfoToFile(aSongInfo, gsLocal_2);
//                                    MSTRING("CreateSongList F " + g4Local_1 + " " + gsLocal_2 + " " + aSongInfo.WorksName);
                                }
                                else {
                                    gDone = 2;
//                                    MSTRING("CreateSongList 1b " + gsLocal_2);
                                }
                                ++g4Local_1;
                            }
                        }
                    }
                    if (mode == 2 && mAUserInfo.songIDs != null) {                      // 2=更多
                        g4Local_1 = mAUserInfo.songIDs.size();
                        aUserInfo = kgAppBase.GetUserFullInfo(mAUserInfo.userID, mAUserInfo);
//                        MSTRING("CreateSongList 2 "+aUserInfo);
                        if (aUserInfo != null && mAUserInfo.songIDs != null) {
                            gDone = 1;
                            newUserInfoFile.AddInfoToFile(aUserInfo, aUserInfo.userID);
                            while (g4Local_1 < aUserInfo.songIDs.size()) {
                                gsLocal_2 = aUserInfo.songIDs.get(g4Local_1);
                                aSongInfo = kgAppBase.GetSongInfo(gsLocal_2);
                                if (aSongInfo != null){
                                    infoFile.AddInfoToFile(aSongInfo, gsLocal_2);
//                                    MSTRING("mUserSongFile.AddInfoToFile 2 " + g4Local_1 + " " + gsLocal_2 + " " + aSongInfo.WorksName);
                                }
                                else {
//                                    MSTRING("mUserSongFile.AddInfoToFile 2e " + g4Local_1 + " " + gsLocal_2);
                                    gDone = 2;
                                }
                                ++g4Local_1;
                            }
                        }
                    }
                }
                if (mDone != null){
//                    MBAPP.MSTRING("mUserSongFile.AddInfoToFile 3 " + mMode +" "+gDone);
                    MSTRING("CreateSongList F");
                    mDone[0] = gDone;
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
    public void TaskClearCommentQty(BTaskRun bTaskRun) {
//        CFileDbs commentQtyDbs = new CFileDbs(bTaskRun.OwnApp.gsCommentQtyDbs);
//        commentQtyDbs.SetUserID4Byte(bTaskRun.OwnApp.gsUserID, 0);
//        bTaskRun.SuccessQty = 0;
//        commentQtyDbs.close();
    }
    public String GetGiftComment(){
        SysUtils sysUtils = new SysUtils();
        return sysUtils.readTextFile(MRAM.gsFilePath + "GiftComment.txt");
    }
    public void WriteGiftComment(String gsLocal_1){
        String gsLocal_2 = MRAM.gsFilePath + "GiftComment.txt";
        MAPI.WRITE_BUFF8_FILE(false, gsLocal_1.getBytes().length, 0, gsLocal_1.getBytes(), gsLocal_2);
    }
    public String[] GiftCommentSplit(String gsLocal_1){
        String[] lineParams = null;
        gsLocal_1 = gsLocal_1.replace(",", "\n");
        gsLocal_1 = gsLocal_1.replace(".", "\n");
        gsLocal_1 = gsLocal_1.replace(";", "\n");
        gsLocal_1 = gsLocal_1.replace("，", "\n");
        gsLocal_1 = gsLocal_1.replace("。", "\n");
        gsLocal_1 = gsLocal_1.replace("；", "\n");
        if (!"".equals(gsLocal_1)) {
            lineParams = gsLocal_1.split("\n");
        }
        return lineParams;
    }

}
